<template>
	<view>
		<u-popup :show="show" mode="bottom" :round="10" @close="close" @open="open">
			<view class="box p-30 flex ai-c flex-col">
				<view class="title ft-30 m-b-50">选择文件导入方式</view>
				<view class="flex" style="width: 80%">
					<view class="icon-item flex flex-col ai-c" @click="chooseFile()">
						<u-image src="@/static/icon/local.png" width="60" height="60"></u-image>
						<view class="m-t-10 ft-26">本地文件</view>
					</view>
					<view class="icon-item flex flex-col ai-c" @click="chooseFile('wx')">
						<u-image src="@/static/icon/weixin.png" width="60" height="60"></u-image>
						<view class="m-t-10 ft-26">微信文件</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
export default {
	props: {
		task: {
			type: Object,
			default: {}
		}
	},
	data() {
		return {
			show: false
		};
	},
	methods: {
		chooseFile(from = '') {
			let _this = this;
			let appConfig = this.$cache.get('app_config');
			console.log('appConfig',appConfig);
			let config = JSON.parse(appConfig) 
			let maxUploadSizePerFile = config.maxUploadSizePerFile;
			if ('wx' === from) {
				wx.chooseMessageFile({
					count: 1,
					type: 'file',
					extension: _this.task.ext || ['pdf'],
					success: ({ tempFiles }) => {
						let tmplFile = tempFiles[0];
						console.log(tmplFile.name);
						let fileMB = parseInt(tmplFile.size / 1024 / 1024)
						if (fileMB > maxUploadSizePerFile) {
							_this.$util.Tips({ title: '文件大小不能超过' })
							return;
						}
						
						_this.$emit('selectComplete', { tempFiles })
						_this.close()
					}
				});
			} else {
				uni.chooseFile({
				        extension:_this.task.ext,
						count: 1, // 最多选择1个文件
				        success: (res) => {
							let tempFiles = res.tempFiles;
							console.log('appConfig fileMB',fileMB);
							let fileMB = parseInt(tempFiles.size / 1024 / 1024)
							if (fileMB > maxUploadSizePerFile) {
								_this.$util.Tips({ title: '文件大小不能超过3M,请使用Pro版本' })
								return;
							}
							_this.$emit('selectComplete', { tempFiles })
							_this.close()
				        },
				        fail: (err) => {
				          console.error('选择文件失败', err);
				        }
				      });
			}
		},
		open() {
			this.show = true;
		},
		close() {
			this.show = false;
		}
	}
};
</script>

<style lang="scss" scoped>
.title {
	width: 100%;
	text-align: center;
}
.icon-item {
	width: 50%;
}
</style>
